package com.gxa.user.recycling.dao.impl;

import com.gxa.core.recycling.pojo.Banner;
import com.gxa.user.recycling.dao.BannerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("bannerDao")
public class BannerDaoImpl implements BannerDao {

    @Autowired
    private MongoTemplate mongoTemplate=null;

    @Override
    public void insertOrUpdate(Banner banner) {
        Query query=Query.query(Criteria.where("id").is(banner.getId()));

        Update update=new Update();
        update.set("title",banner.getTitle());
        update.set("description",banner.getDescription());
        update.set("image",banner.getImage());
        /*
        update.set("position",banner.getPosition());
        update.set("image",banner.getImage());
        update.set("url",banner.getUrl());
        update.set("created",banner.getCreated());
        update.set("hot",banner.getHot());*/

        mongoTemplate.upsert(query,update,Banner.class);


    }

    @Override
    public List<Banner> find(Banner banner) {
        //mongoTemplate.find()

        /*Query query=new Query(Criteria.where("position").is(banner.getPosition()));

        return mongoTemplate.find(query,Banner.class);*/
        return null;
    }

    @Override
    public List<Banner> findAll() {
        return mongoTemplate.findAll(Banner.class);
    }

    @Override
    public void delete(Banner banner) {


        //可以改进为判断非空的判断,根据
        Query query=new Query(Criteria.where("id").is(banner.getId()));

        mongoTemplate.remove(query,Banner.class);
    }
}
